package com.application.sistercityapplication.gallery;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.application.sistercityapplication.dto.GalleryDto;
import com.application.sistercityapplication.dto.PictureDto;
import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class GridViewImageAdapter extends BaseAdapter {

	private Activity _activity;
	private List<PictureDto> gallerys = new ArrayList<PictureDto>();
	private int imageWidth;
	private List<String> _filePaths = new ArrayList<String>();
	

	public GridViewImageAdapter(Activity activity, List<PictureDto> gallerys,
			int imageWidth) {
		this._activity = activity;
		this.gallerys = gallerys;
		this.imageWidth = imageWidth;
	}

	@Override
	public int getCount() {
		return this.gallerys.size();
	}

	@Override
	public Object getItem(int position) {
		return this.gallerys.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Bitmap image = null;
		ImageView imageView;
		if (convertView == null) {
			imageView = new ImageView(_activity);
		} else {
			imageView = (ImageView) convertView;
		}

		// get screen dimensions
		new DownloadGalleryList(_activity, position,image,imageView).execute();
		

		// image view click listener
		imageView.setOnClickListener(new OnImageClickListener(position));

		return imageView;
	}

	class OnImageClickListener implements OnClickListener {

		int _postion;

		// constructor
		public OnImageClickListener(int position) {
			this._postion = position;
		}

		@Override
		public void onClick(View v) {
			// on selecting grid view image
			// launch full screen activity
			for(PictureDto dto : gallerys){
				_filePaths.add(dto.getPath());
			}
			Intent i = new Intent(_activity, FullScreenViewActivity.class);
			i.putExtra("position", _postion);
			i.putStringArrayListExtra("paths",new ArrayList<String>(_filePaths));
			_activity.startActivity(i);
		}

	}

	/*
	 * Resizing image size
	 */
	public static Bitmap decodeFile(String url, int WIDTH, int HIGHT) throws IOException {
		try {
			URL urls = new URL(url);
			//File f = new File(filePath);

			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(urls.openConnection().getInputStream(), null, o);

			final int REQUIRED_WIDTH = WIDTH/2;
			final int REQUIRED_HIGHT = HIGHT/2;
			int scale = 1;
			while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
					&& o.outHeight / scale / 2 >= REQUIRED_HIGHT)
				scale *= 2;

			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			return BitmapFactory.decodeStream(urls.openConnection().getInputStream(), null, o2);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	public class DownloadGalleryList extends AsyncTask<String, Void, Bitmap> {
		//ProgressBar pDialog;
		private Context context;
		public Activity send;
		private int _position;
		private Bitmap _map;
		private ImageView _ima;

		public DownloadGalleryList(Activity aa,int position,Bitmap map,ImageView ima) {
			super();
			this.send = aa;
			this.context = aa;
			this._position = position;
			this._map = map;
			this._ima = ima;
			
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
//			pDialog = new ProgressDialog(_activity);
//			pDialog.setMessage("Loading...");
//			pDialog.show();
			
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			try {
				
				_map = decodeFile(gallerys.get(_position).getThumbnail(), imageWidth,imageWidth);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return _map;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
//			if (null != pDialog && pDialog.isShowing()) {
//				pDialog.dismiss();
//			}

			if (result != null) {
				_ima.setScaleType(ImageView.ScaleType.CENTER_CROP);
				_ima.setLayoutParams(new GridView.LayoutParams(imageWidth,imageWidth));
				_ima.setImageBitmap(result);
			} else {
				(Toast.makeText(context, "An error occured.", Toast.LENGTH_LONG)).show();
			}

		}

	}
}
